package ca.cbc.localsrvextadmin.filter;

import java.io.IOException;

import javax.servlet.Filter;
import javax.servlet.FilterChain;
import javax.servlet.FilterConfig;
import javax.servlet.ServletException;
import javax.servlet.ServletRequest;
import javax.servlet.ServletResponse;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import ca.cbc.giscommon.util.HibernateUtil;

public class LSEAdminFilter implements Filter {
	private static Logger logger = LoggerFactory.getLogger(LSEAdminFilter.class);

	public void init(FilterConfig filterConfig) throws ServletException {
		logger.info("------ LocalSrvExtAdmin Starting Up -------");
		HibernateUtil.createSessionFactory();
	}

	public void doFilter(ServletRequest request, ServletResponse response,
			FilterChain chain) throws IOException, ServletException {
		HibernateUtil.beginTransaction();
		chain.doFilter(request,response);	
		if(HibernateUtil.isActiveTransaction())
			HibernateUtil.commitTransaction();
	}

	public void destroy() {
		logger.info("====== LocalSrvExtAdmin Shuting down =====");
		HibernateUtil.shutdown();
	}

}
